<!--
 * @Description: 宴席套餐管理
 * @Author: xiao li
 * @Date: 2021-11-18 10:47:05
 * @LastEditTime: 2022-01-24 18:12:55
 * @LastEditors: xiao li
-->

<template>
  <div class="lb-car-list">
    <top-nav />
    <div class="page-main">
      <el-row class="page-top-operate">
        <lb-button
          size="medium"
          type="primary"
          opType="add"
          icon="el-icon-plus"
          @click="$router.push(`${appPath}/banquet/list/edit`)"
          >{{ $t('menu.BanquetListAdd') }}</lb-button
        >
      </el-row>

      <div class="page-search-form">
        <el-form
          @submit.native.prevent
          :inline="true"
          :model="searchForm"
          ref="searchForm"
        >
          <el-form-item label="输入查询" prop="title">
            <el-input
              placeholder="请输入宴席套餐名称"
              v-model="searchForm.title"
            ></el-input>
          </el-form-item>
          <el-form-item label="所属公司" prop="company_id">
            <el-select
              @change="getTableDataList(1)"
              v-model="searchForm.company_id"
              filterable
              placeholder="请选择所属公司"
            >
              <el-option
                v-for="item in base_company"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <lb-button
              size="medium"
              type="primary"
              icon="el-icon-search"
              style="margin-right: 5px"
              @click="getTableDataList(1)"
              >{{ $t('action.search') }}</lb-button
            >
            <lb-button
              size="medium"
              icon="el-icon-refresh-left"
              style="margin-right: 5px"
              @click="resetForm('searchForm')"
              >{{ $t('action.reset') }}</lb-button
            >
          </el-form-item>
        </el-form>
      </div>
      <el-table
        v-loading="loading"
        :data="tableData"
        :header-cell-style="{ background: '#f5f7fa', color: '#606266' }"
        tooltip-effect="dark"
        style="width: 100%"
      >
        <el-table-column prop="id" label="ID" fixed> </el-table-column>
        <el-table-column prop="title" label="宴席套餐名称" min-width="120">
        </el-table-column>
        <el-table-column prop="cover" label="封面图">
          <template slot-scope="scope">
            <lb-image :src="scope.row.cover" />
          </template>
        </el-table-column>
        <el-table-column prop="price" label="套餐价格" min-width="120">
          <template slot-scope="scope">
            {{ `¥${scope.row.price}` }}
          </template>
        </el-table-column>
        <el-table-column prop="price" label="宴会价格" min-width="120">
          <template slot-scope="scope">
            {{ `¥${scope.row.banquet_price}` }}
          </template>
        </el-table-column>
        <!-- <el-table-column prop="price" label="所属公司" min-width="120">
          <template slot-scope="scope">
            {{ `¥${scope.row.price}` }}
          </template>
        </el-table-column> -->
        <el-table-column prop="top" label="排序值"></el-table-column>
        <el-table-column prop="status" label="是否上架">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.status"
              :active-value="1"
              :inactive-value="0"
              @change="updateItem(scope.row.id, scope.row.status)"
            >
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column prop="create_time" label="创建时间" min-width="120">
          <template slot-scope="scope">
            <div>{{ scope.row.create_time | handleTime(1) }}</div>
            <div>{{ scope.row.create_time | handleTime(2) }}</div>
          </template>
        </el-table-column>
        <el-table-column label="操作" min-width="140">
          <template slot-scope="scope">
            <div class="table-operate">
              <lb-button
                size="mini"
                plain
                type="primary"
                @click="
                  $router.push(
                    `${appPath}/banquet/list/edit?id=${scope.row.id}`
                  )
                "
                >{{ $t('action.edit') }}</lb-button
              >
              <lb-button
                size="mini"
                plain
                type="danger"
                @click="confirmDel(scope.row.id)"
                >{{ $t('action.delete') }}</lb-button
              >
            </div>
          </template>
        </el-table-column>
      </el-table>

      <lb-page
        :batch="false"
        :page="searchForm.page"
        :pageSize="searchForm.limit"
        :total="total"
        @handleSizeChange="handleSizeChange"
        @handleCurrentChange="handleCurrentChange"
      >
      </lb-page>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      appPath: '',
      base_company: [],
      loading: false,
      searchForm: {
        page: 1,
        limit: 10,
        title: '',
        company_id: '',
      },
      tableData: [],
      total: 0
    }
  },
  async activated () {
    let { path } = this.$route
    this.appPath = path.includes('app/') ? '/app' : ''
    await this.getBaseInfo()
    this.getTableDataList()
  },
  methods: {
    async getBaseInfo () {
      let { data } = await this.$api.getBaseCompany()
      this.base_company = data
    },
    resetForm (name) {
      this.$refs[name].resetFields()
      this.getTableDataList(1)
    },
    handleSizeChange (val) {
      this.searchForm.limit = val
      this.handleCurrentChange(1)
    },
    handleCurrentChange (val) {
      this.searchForm.page = val
      this.getTableDataList()
    },
    /**
     * @method: 获取列表
     */
    async getTableDataList (flag) {
      if (flag) this.searchForm.page = 1
      this.loading = true
      this.$api.banquet.packageList(this.searchForm).then(res => {
        this.loading = false
        if (res.code === 200) {
          this.tableData = res.data.data
          this.total = res.data.total
          this.on_count = res.data.on_count
          this.off_count = res.data.off_count
        }
      })
    },
    /**
     * @method: 删除
     * @param {*} id
     */
    confirmDel (id) {
      this.$confirm(this.$t('tips.confirmDelete'), this.$t('tips.reminder'), {
        confirmButtonText: this.$t('action.comfirm'),
        cancelButtonText: this.$t('action.cancel'),
        type: 'warning'
      }).then(() => {
        this.updateItem(id, -1)
      })
    },
    /**
     * @method: 上下架
     */
    async updateItem (id, status) {
      this.$api.banquet.packageUpdate({ id, status }).then(res => {
        if (res.code === 200) {
          this.$message.success(this.$t(status === -1 ? 'tips.successDel' : 'tips.successOper'))
          if (status !== -1) return
          this.searchForm.page = this.searchForm.page < Math.ceil((this.total - 1) / this.searchForm.limit) ? this.searchForm.page : Math.ceil((this.total - 1) / this.searchForm.limit)
          this.getTableDataList()
        } else {
          if (status === -1) return
          this.getTableDataList()
        }
      })
    }
  },
  filters: {
    handleTime (val, type) {
      let time = type === 1 ? moment(val * 1000).format('YYYY-MM-DD') : type === 2 ? moment(val * 1000).format('HH:mm:ss') : moment(val * 1000).format('YYYY-MM-DD HH:mm:ss')
      return time
    }
  }
}
</script>

<style lang="scss" scoped>
.lb-appclass-course-type {
  width: 100%;
  .page-main {
    width: 100%;
    padding: 20px;
    .el-input,
    .el-select,
    .el-input-number {
      width: 200px;
    }
  }
}
</style>
